package com.shenhaofeng.demo.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.Button;

import com.shenhaofeng.app.camera.Camera;
import com.shenhaofeng.app.camera.Option;
import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.component.dialog.ImageSourceItemDialog;
import com.shenhaofeng.app.utils.toast.T;
import com.shenhaofeng.demo.R;

import java.io.File;

/**
 * Created by haofeng on 2016/9/26.
 */

public class ImageSourceActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_image_source);
        Button cameraBtn = (Button) findViewById(R.id.camera);
        Button albumBtn = (Button) findViewById(R.id.album);
        Button selectImageBtn = (Button) findViewById(R.id.select_image);
        cameraBtn.setOnClickListener(this);
        albumBtn.setOnClickListener(this);
        selectImageBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        File file=new File(getFilesDir(),"images");
        file=new File(file,"test.jpg");
        switch (v.getId()) {
            case R.id.camera: {
                openCamera(1, new Option.Builder().outputUri(FileProvider.getUriForFile(this,"com.shenhaofeng.demo.fileprovider",file)).build());
                //或者调用 getCameraController().openCamera()
                break;
            }
            case R.id.album: {
                openAlbum(2);
                //或者调用 getCameraController().openAlbum();
                break;
            }
            case R.id.select_image: {
                ImageSourceItemDialog.createInstance("选择数据源",FileProvider.getUriForFile(this,"com.shenhaofeng.demo.fileprovider",file), 1, 2, 0).show(getSupportFragmentManager(), null);
                break;
            }
        }
    }

    @Override
    protected boolean onCameraResponse(int request, boolean result, Camera camera) {
        if(result){
            T.show(this, camera.getPhotoUri().toString());
        }else{
            T.show(this,"取消");
        }
        return true;
    }

    @Override
    protected boolean onAlbumResponse(int request, boolean result, Uri data) {
        if(result){
            T.show(this, data.toString());
        }else{
            T.show(this,"取消");
        }
        return true;
    }
}
